#!/bin/bash

# 
APPNAME=blogapp
BASEDIR=`pwd`/`dirname $0`
APPROOT=$BASEDIR/$APPNAME
WORKDIR=$APPROOT/_work.$$
DBFILE=$APPROOT/db/dbfile
RANDOMTXT="1234567890123456asdfghjklLKJHGFDSA_____"
TREEFROG=treefrog
TSPAWN=tspawn
PORT=8885

# Function
row_count()
{
  echo "SELECT COUNT(1) FROM blog;" | sqlite3 $DBFILE
}

# Cleanup
cleanup()
{
  # Cleanup
  cd $APPROOT
  $TREEFROG -k abort >/dev/null 2>&1

  cd $BASEDIR
  rm -rf $WORKDIR
  rm -rf $APPNAME
}

## Main ##
trap 'cleanup' 2 3 15 EXIT

# Create app
cd $BASEDIR
if [ -d "$APPNAME" ]; then
  $TREEFROG -k abort $APPNAME
  rm -rf $APPNAME
fi

$TSPAWN new $APPNAME
sqlite3 $DBFILE < create_blog_table.sql || exit

# Build
cd $APPROOT
$TSPAWN s blog || exit
$TSPAWN ms fuga || exit
$TSPAWN w foo || exit
qmake -r || exit
make -j4
make || exit

# Start Check
$TREEFROG -e dev -p $PORT -d || exit
sleep 1
$TREEFROG -k stop || exit
sleep 1
$TREEFROG -e dev -p $PORT -d || exit
sleep 1

# GET method
mkdir -p $WORKDIR
cd $WORKDIR
wget http://localhost:$PORT/blog/index || exit
wget http://localhost:$PORT/blog/create || exit

# POST method
# create1
curl --data-urlencode 'blog[title]=hello' --data-urlencode 'blog[body]=Hello world.' http://localhost:$PORT/blog/create > create1 || exit
[ `row_count` = "1" ] || exit

wget http://localhost:$PORT/blog/show/1 || exit

# create2
curl --data-urlencode 'blog[title]=Hi!' --data-urlencode 'blog[body]=Hi, all' http://localhost:$PORT/blog/create > create2 || exit
[ `row_count` = "2" ] || exit

wget http://localhost:$PORT/blog/show/2 || exit

# update
curl --data-urlencode 'blog[id]=1' --data-urlencode 'blog[title]=Hi!' --data-urlencode "blog[body]=$RANDOMTXT" http://localhost:$PORT/blog/save/1 > save1 || exit
[ `row_count` = "2" ] || exit

# update check
wget http://localhost:$PORT/blog/show/1 -O show1 || exit
if ! grep "$RANDOMTXT" show1 >/dev/null 2>&1; then exit; fi

# delete
curl  --data-urlencode 'dummy'  http://localhost:$PORT/blog/remove/1  > remove1 || exit
[ `row_count` = "1" ] || exit

curl  --data-urlencode 'dummy'  http://localhost:$PORT/blog/remove/2  > remove2 || exit
[ `row_count` = "0" ] || exit

# Stop app
cd $APPROOT
$TREEFROG -k stop || exit
echo
echo "Test completed."
echo "Congratulations!"

#EOF
